package com.ms.module.wechat.clear.repository;

import android.content.Context;
import android.content.SharedPreferences;

import com.ms.module.wechat.clear.provider.InitProvider;

import java.util.Random;

public class SharedPreferencesData {
    private static final String NAME = "com_ms_wechat_clear_module";
    private static final SharedPreferencesData instance = new SharedPreferencesData();
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor edit;

    public static SharedPreferencesData getInstance() {
        sharedPreferences = InitProvider.get().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        edit = sharedPreferences.edit();
        return instance;
    }

    public void put(String k, String v) {
        edit.putString(k, v).apply();
    }

    public String get(String k) {
        return sharedPreferences.getString(k, "");
    }


    private static final Random random = new Random(1024 * 1024 * 50);


    public static void updateLastTimestamp() {
        getInstance().put("last_timestamp", System.currentTimeMillis() + "");
    }


    public static boolean is30Min() {
        String last_timestamp = getInstance().get("last_timestamp");
        if (null == last_timestamp || "".equals(last_timestamp)) {
            return false;
        }

        Long time = Long.valueOf(last_timestamp);


        if (null == time) {
            return false;
        }


        long currentTimeMillis = System.currentTimeMillis();

        if (currentTimeMillis - time < 1000 * 60 * 30) {
            return true;
        }
        return false;
    }


    public static long random() {
        return new Random().nextInt(1024 * 1024 * 50);

        // return random.nextInt(1024 * 1024 * 50);
    }


    public static boolean isRandomValue = false;


    // 判断你是否需要假数据
    public boolean isRandomValue() {
        return !is30Min() && isRandomValue;

    }

}
